/** * 头部菜单 */
<template>
  <el-menu class="el-menu-demo" mode="horizontal" background-color="#334157" text-color="#fff" active-text-color="#fff">
    <el-button class="buttonimg" @click="toggle(collapsed)">
      <img class="showimg" :src="collapsed ? imgsq : imgshow" />
      <!-- <span class="VR_student">VR教学管理平台</span> -->
    </el-button>

    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" class="submenuIn">
      <template v-if="this.$route.matched[1].name != 'overviewOf'">
        <el-breadcrumb-item v-for="(item,index) in navList" :key="item.path">
          <router-link :to="item.path" style="color: #fff;font-weight: normal;cursor: pointer;text-decoration: none;" :class="{'act': index == navList.length - 1}">{{ item.meta.theTitle }} </router-link>
        </el-breadcrumb-item>
      </template>
      <template v-else>
        <el-breadcrumb-item> <span style="cursor: text !important; color: #ccc !important; text-decoration: none;">概览</span> </el-breadcrumb-item>
      </template>
    </el-breadcrumb>

    <!-- 头部右侧 -->
    <el-submenu index="2" class="submenu">
      <!-- <template slot="title">{{user.userRealName}}</template> -->
      <template slot="title"><i class="el-icon-lock"></i>超级管理员</template>
      <el-menu-item @click="exit()" index="2-3"><img src="../assets/img/out.png" class="outInTo">退出</el-menu-item>
    </el-submenu>
    <el-menu-item class="submenu">
      <i class="el-icon-s-custom"></i>
      <span slot="title">admin</span>
    </el-menu-item>
  </el-menu>
</template>

<script>
import { loginout } from '../api/userMG'
import storage from 'store'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
export default {
  name: 'navcon',
  data() {
    return {
      collapsed: true,
      imgshow: require('../assets/img/show.png'),
      imgsq: require('../assets/img/sq.png'),
      user: {},
      navList: [],
    }
  },
  // 这里必须使用监听，否则无法实时获取路由变动信息。
  // 监听后路由会实时变动，不然需要手动刷新路径才会改变
  watch:{
    $route(to,from)  {
      // console.log(to,from)
      if(to.fullPath != '/overviewOf') { this.getBreadcrumb(); }
      if(to.fullPath == '/content/curriculum' && from.fullPath == '/content/details') {
        this.newMatched[1] = { path: '/content/details', meta: { title: '课件列表详情',theTitle: '课件列表详情' } };
        this.newMatched[2] = { path: '/content/curriculum', meta: { title: '编辑课件',theTitle: '编辑课件' } };
        this.navList = this.newMatched;
      } else if(to.fullPath == '/theUser/newUsers' && from.fullPath == '/theUser/userDetails') {
        this.newMatched[1] = { path: '/theUser/userDetails', meta: { title: '用户详情',theTitle: '用户详情' } }
        this.newMatched[2] = { path: '/theUser/newUsers', meta: { title: '编辑用户',theTitle: '编辑用户' } };
        this.navList = this.newMatched;
      } else if(to.fullPath == '/content/details' && from.fullPath == '/equipment/eqDetails') {  // 这个是设备详情中详情跳转过来
        const newDasss = sessionStorage.getItem('userData');
        this.newMatched[0] = { path: '/equipment', meta: { title: '设备',theTitle: '设备' } };
        this.newMatched[1] = { path: '/equipment/eqDetails', meta: { title: '设备详情',theTitle: '设备详情' } };
        this.newMatched[2] = { path: '/content/details', meta: { title: '设备课件详情',theTitle: '设备课件详情' } };
        this.navList = this.newMatched;
        sessionStorage.setItem('userData',newDasss);
      } else if(to.fullPath == '/setUpThe/versionUpdate' && from.fullPath == '/setUpThe') {
        if(sessionStorage.getItem('routerThis')) {
          this.newMatched[1] = { path: '/setUpThe/versionUpdate', meta: { title: '版本设置详情',theTitle: '版本设置详情' } };
          this.navList = this.newMatched;
        } else { this.navList = this.newMatched; }
      } else if(to.fullPath == '/content/details' && from.fullPath == '/overviewOf') {
        this.newMatched[0] = { path: '/overviewOf', meta: { title: '概览',theTitle: '概览' } };
        this.navList = this.newMatched;
      } else if(to.fullPath == '/content' && from.fullPath == '/content/details') { sessionStorage.removeItem('userData'); }
      else if(to.fullPath == '/overviewOf' && from.fullPath == '/content/details') {sessionStorage.removeItem('userDataS');}
      else if(to.fullPath == '/content' && from.fullPath == '/content/curriculum') { sessionStorage.removeItem('add'); }
      else if(to.name == 'learnDetails' || to.name == 'userDetails' || to.name == 'theUser' && from.name == 'theReport') {
        sessionStorage.removeItem('reportRi');
      } else if(to.name == 'setUpThe' && from.name == 'versionUpdate'){
        sessionStorage.removeItem('versionAdd')
      }
    }
  },
	mounted() {
		NProgress.start();
		setTimeout( () => {
			NProgress.done();
		},1000)
	},
  // 创建完毕状态(里面是操作)
  created() {
    this.user = JSON.parse(localStorage.getItem('userdata'));
    // this.navList = this.$route.matched  //  获取路由内的全部信息
    if(this.$route.matched[1].name != "overviewOf") {
      this.getBreadcrumb();
    } else {
      this.navList = this.$route.matched;
    }
  },
  methods: {
    // 退出登录
    exit() {
      this.$confirm('退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          storage.remove('logintoken')
          storage.remove('userInfo')
          setTimeout(() => {
            window.location.reload()
          }, 10)
          // setTimeout(() => {
          //   this.$store.commit('logout', 'false')
          //   this.$router.push({ path: '/login' })
          //   this.$message({
          //     type: 'success',
          //     message: '已退出登录!'
          //   })
          // }, 1000)
          // loginout()
          //   .then(res => {
          //     if (res.success) {
          //       // 如果请求成功就让他2秒跳转路由
          //       setTimeout(() => {
          //         this.$store.commit('logout', 'false')
          //         this.$router.push({ path: '/login' })
          //         this.$message({
          //           type: 'success',
          //           message: '已退出登录!'
          //         })
          //       }, 1000)
          //     } else {
          //       this.$message.error(res.msg)
          //       this.logining = false
          //       return false
          //     }
          //   })
          //   .catch(err => {
          //     // 获取图形验证码
          //     this.getcode()
          //     this.logining = false
          //     this.$message.error('退出失败，请稍后再试！')
          //   })
        })
        .catch(() => {})
    },
     getBreadcrumb() {
      // matched 包含当前路由的所有嵌套路径片段的路由记录<Array>
      let matched = this.$route.matched.filter(item => item.meta && item.meta.theTitle);
      this.newMatched = matched;
      // console.log(matched)
      // const first = this.newMatched[0];
      // 默认显示Home,可根据项目自行调整
      // if (!this.isHome(first)) {
      //   this.newMatched = [{ path: '/overviewOf', meta: { title: '概览',theTitle: '概览' } }].concat(matched)
      // }
      this.navList = this.newMatched;
    },
    isHome(route) {
      const name = route && route.name
      if (!name) {
        return false
      }
      return name.trim().toLocaleLowerCase() === 'index';
    },
    // 切换显示
    toggle(showtype) {
      this.collapsed = !showtype;
      this.$root.Bus.$emit('toggle', this.collapsed);
    }
  }
}
</script>

<style scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) { border: none; }
.submenu { float: right; }
.el-breadcrumb{ line-height: 25px; }
.submenuIn{ position: absolute;top: 20px;left: 60px;font-size: 18px; }
.submenuIn >>> .el-breadcrumb__item >>> .el-breadcrumb__inner{ color: #fff !important; }
.buttonimg { height: 60px; background-color: transparent; border: none; }
.showimg { width: 26px; height: 26px; position: absolute; top: 17px; left: 17px; }
.showimg:active { border: none; }
.VR_student { margin-left: 35px; font-size: 18px; color: #fff; font-weight: 600; }
.outInTo{ width: 20px; height: 20px;margin-right: 15px; }
.el-menu--horizontal .el-menu .el-menu-item{ padding: 0 20px;color: #fff !important; }
.act { cursor: text !important; color: #ccc !important; text-decoration: none;}
</style>